package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import darksideoftheloom.Event;

/**
 * This class is the top level container for the GUI.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 * 
 */
@SuppressWarnings("serial")
public class BeweaveFrame extends JFrame {

    /**
     * The login panel.
     */
    private final BeweaveLoginPanel my_login_panel;

    /**
     * The registration panel.
     */
    private final BeweaveRegisterPanel my_register_panel;

    /**
     * The submission panel.
     */
    private final BeweaveSubmitPanel my_submit_panel;

    /**
     * The Attendee home panel.
     */
    private final BeweaveAttendeePanel my_attendee_panel;

    /**
     * The Judge home panel.
     */
    private final BeweaveJudgePanel my_judge_panel;

    /**
     * The Organizer home panel.
     */
    private final BeweaveOrganizerPanel my_organizer_panel;
    
    /**
     * The Weaving Draft panel.
     */
    private final BeweaveWeaveDraftPanel my_weavingdraft_panel;

    /**
     * The weaving event in question.
     */
    private final Event my_event;

    /**
     * The container for all the other panels.
     */
    private final JPanel my_main_panel;

    /**
     * Constructs a BeweaveFrame and loads the event data.
     */
    public BeweaveFrame() {
        super(
                "Dark Side of the Loom presents: Just Beweave, a TCSS 360 Production");
        my_event = new Event();
        my_event.load();
        my_main_panel = new JPanel(new CardLayout());
        my_login_panel = new BeweaveLoginPanel(my_event, my_main_panel);
        my_register_panel = new BeweaveRegisterPanel(my_event, my_main_panel);
        my_weavingdraft_panel = new BeweaveWeaveDraftPanel(my_main_panel);
        my_submit_panel = new BeweaveSubmitPanel(my_main_panel, my_weavingdraft_panel);
        my_attendee_panel = new BeweaveAttendeePanel(my_main_panel);
        my_judge_panel = new BeweaveJudgePanel(my_event, my_main_panel);
        my_organizer_panel = new BeweaveOrganizerPanel(my_event, my_main_panel);        
    }

    /**
     * Adds all the panels to the panel container.
     */
    private void addCardsToPanel() {
        my_main_panel.add(my_login_panel, "login");
        my_main_panel.add(my_register_panel, "register");
        my_main_panel.add(my_submit_panel, "submit");
        my_main_panel.add(my_attendee_panel, "attendee");
        my_main_panel.add(my_judge_panel, "judge");
        my_main_panel.add(my_organizer_panel, "organizer");
        my_main_panel.add(my_weavingdraft_panel.getPanel(), "draft");
    }

    /**
     * Creates a panel to display information and/or pictures at the top of the
     * application frame.
     * 
     * @return a JPanel with text or visual data
     */
    private JPanel createBannerPanel() {
        JPanel result = new JPanel();

        // TODO replace placeholder
        JLabel placeholder = new JLabel("Banner image to come...");
        result.add(placeholder);

        return result;
    }

    /**
     * Places panels in correct layout and makes frame visible.
     */
    public void start() {
        setLayout(new BorderLayout());
        addCardsToPanel();

        add(createBannerPanel(), BorderLayout.NORTH);
        add(my_main_panel, BorderLayout.CENTER);

        // TODO button is for testing only, remove when no longer needed
//        add(new JButton(new AbstractAction("change panel") {
//            public void actionPerformed(final ActionEvent the_event) {
//                CardLayout cl = (CardLayout) my_main_panel.getLayout();
//                cl.next(my_main_panel);
//            }
//        }), BorderLayout.SOUTH);

        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
}
